package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author Jonatas Rubim
	 */
	public class Main extends MovieClip
	{
		var char:Char;
		var frente:Boolean;
		var tras:Boolean;
		var baixo:Boolean;
		var pulo:Boolean;
		var ataque:Boolean;
		var animando:Boolean = false;
		
		public function Main()
		{
			
			frente = false;
			tras = false;
			baixo = false;
			pulo = false;
			ataque = false;
			desenharTela();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressionar);
			stage.addEventListener(KeyboardEvent.KEY_UP, soltar);
			stage.addEventListener(Event.ENTER_FRAME, atualizar);
		}


		public function desenharTela():void
		{
			char = new Char();
			char.x = 100;
			char.y = stage.stageHeight / 2;
			char.stop();
			addChild(char);
		}


		public function pressionar(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.RIGHT)
			{
				frente = true;
			}
			else if (e.keyCode == Keyboard.LEFT)
			{
				tras = true;
			}
		}


		public function soltar(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.RIGHT)
			{
				frente = false;
				char.gotoAndStop(1);
				animando = false;
			}
			if (e.keyCode == Keyboard.LEFT)
			{
				tras = false;
				char.gotoAndStop(1);
				animando = false;
			}
		}


		public function atualizar(e:Event):void
		{
			
			controlaPersonagem();
			
		}


		public function controlaPersonagem():void
		{
			if (frente)
			{
				char.x += 10
				char.scaleX = 1;
				animar();
			}
			else
			if (tras)
			{
				char.x -= 10;
				char.scaleX = -1;
				animar();
				
			}
		}
		public function animar():void
		{
			if (char.currentFrame >= 6)
				char.gotoAndPlay("andando");
			if (!animando)
			{
				animando = true;
				char.gotoAndPlay("andando");
			}
		}
	}
}	